//
// Query remote device's information.
// Combine client and DiscoveryListener into one class
// compare this program with RemoteInfo and MyListener
// Advantage: main() can use queried remote device directly
//

import java.io.*;
import javax.bluetooth.*;
import javax.microedition.io.*;

public class RemoteOne implements DiscoveryListener {
	static final Object lockObject = new Object();

	public static void main(String args[]) throws BluetoothStateException {
		LocalDevice localdev = null;
		RemoteOne listener = new RemoteOne();

		try {
			localdev = LocalDevice.getLocalDevice();
		} catch (BluetoothStateException e) {
			System.out.println("ERROR: cannot access local device");
			System.exit(1);
		}

		DiscoveryAgent agent = localdev.getDiscoveryAgent();
		synchronized (lockObject) {

			try {
				agent.startInquiry(DiscoveryAgent.GIAC, listener);
				lockObject.wait();
			} catch (BluetoothStateException e) {
				System.out.println("Device unable to inquiry");
				System.exit(2);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	// implement required methods.
	private RemoteDevice remote = null;

	public void deviceDiscovered(RemoteDevice rt, DeviceClass cod) {
		remote = rt;
		System.out.println("A Remote Device Found:");
		System.out.println("Address: " + rt.getBluetoothAddress());
		try {
			System.out.println("   Name: " + rt.getFriendlyName(true));
		} catch (IOException e) {
		}
	}

	public void inquiryCompleted(int distype) {
		if (distype == INQUIRY_TERMINATED)
			System.out.println("Application Terminated");
		else if (distype == INQUIRY_COMPLETED)
			System.out.println("Inquiry Completed");
		else
			System.out.println("ERROR: Inquiry aborted");
		synchronized (lockObject) {
			lockObject.notifyAll();
		}
	}

	public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
	}

	public void serviceSearchCompleted(int transID, int respCode) {
	}
}